package net.realsee.controller.wcs.work;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.enums.wcs.order.OrderProcessStatusEnum;
import net.realsee.common.enums.wcs.order.OrderStatusEnums;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.work.bo.WcsOrderHeaderBo;
import net.realsee.domain.wcs.work.vo.WcsOrderHeaderVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.service.wcs.work.IWcsOrderHeaderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 单据头
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/orderHeader")
public class WcsOrderHeaderController extends BaseController {

    private final IWcsOrderHeaderService iWcsOrderHeaderService;

    /**
     * 查询单据头列表
     */
    @SaCheckPermission("wcs:orderHeader:list")
    @GetMapping("/list")
    public TableDataInfo<WcsOrderHeaderVo> list(WcsOrderHeaderBo bo, PageQuery pageQuery) {
        return iWcsOrderHeaderService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出单据头列表
     */
    @SaCheckPermission("wcs:orderHeader:export")
    @Log(title = "单据头", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsOrderHeaderBo bo, HttpServletResponse response) {
        List<WcsOrderHeaderVo> list = iWcsOrderHeaderService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "单据头", WcsOrderHeaderVo.class, response);
    }

    /**
     * 获取单据头详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:orderHeader:query")
    @GetMapping("/{id}")
    public R<WcsOrderHeaderVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iWcsOrderHeaderService.selectVoById(id));
    }

    /**
     * 新增单据头
     */
    @SaCheckPermission("wcs:orderHeader:add")
    @Log(title = "单据头", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsOrderHeaderBo bo) {
        return toAjax(iWcsOrderHeaderService.insertByBo(bo));
    }

    /**
     * 修改单据头
     */
    @SaCheckPermission("wcs:orderHeader:edit")
    @Log(title = "单据头", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsOrderHeaderBo bo) {
        return toAjax(iWcsOrderHeaderService.updateByBo(bo));
    }

    /**
     * 删除单据头
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:orderHeader:remove")
    @Log(title = "单据头", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsOrderHeaderService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 取消任务
     */
    @SaCheckPermission("wcs:orderHeader:cancel")
    @Log(title = "取消任务", businessType = BusinessType.OTHER)
    @PostMapping("/cancel/{orderNo}")
    public R<Void> cancel(@NotEmpty(message = "任务单号不能为空") @PathVariable String orderNo) {
        return toAjax(iWcsOrderHeaderService.cancel(orderNo));
    }

    /**
     * 查询单据状态（大状态）列表
     */
    @GetMapping("/listOrderStatus")
    public R<List<Map<String, Object>>> listOrderStatus() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (OrderStatusEnums orderStatusEnums : OrderStatusEnums.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("dictValue", orderStatusEnums.getCode());
            map.put("dictLabel", orderStatusEnums.getMessage());
            resultList.add(map);
        }
        return R.ok(resultList);
    }

    /**
     * 查询单据状态（小状态）列表
     */
    @GetMapping("/listOrderProcessStatus")
    public R<List<Map<String, Object>>> listOrderProcessStatus() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (OrderProcessStatusEnum orderProcessStatusEnum : OrderProcessStatusEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("dictValue", orderProcessStatusEnum.getCode());
            map.put("dictLabel", orderProcessStatusEnum.getMsg());
            resultList.add(map);
        }
        return R.ok(resultList);
    }
}
